import React from 'react';
import { connect } from 'react-redux';
import menuRoute from 'routes/menuRoute';
import contractService from 'containers/approve/contract/contract.service';
import { Form, Affix, Button, message } from 'antd';
import ContractDetailCommon from 'containers/contract/my-contract/contract-detail-common';
import 'styles/contract/my-contract/contract-detail.scss';

import { messages } from 'share/common';

class ContractDetail extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      dLoading: false,
      submitAble: false,
      headerData: {},
      myContract: menuRoute.getRouteItem('my-contract', 'key'),    //我的合同
    };
  }

  //获取合同状态
  getStatus = (status) => {
    if (status === 1001 || status === 1005 || status === 1003) {
      this.setState({ submitAble: true });
    }
  };

  //提交
  onSubmit = () => {
    this.setState({ loading: true });
    if (this.detail && this.detail.state.headerData.contractName) {
      // 如果formOid有值，则走工作流提交
      if (this.detail.state.headerData.formOid) {
        const { headerData } = this.detail.state;
        let params = {
          'applicantOID': headerData.applicantOid,
          'userOID': this.props.user.userOID,
          'formOID': headerData.formOid,
          'entityOID': headerData.documentOid,
          'entityType': headerData.documentType,
          'countersignApproverOIDs': null
        };
        contractService.submitWorkflowContract(params)
          .then(res => {
            if (res.status === 200) {
              this.setState({ loading: false });
              message.success(messages('common.operate.success'/*操作成功*/));
              this.onCancel();
            }
          })
          .catch(e => {
            this.setState({ loading: false });
            message.error(`${messages('common.operate.filed'/*操作失败*/)}，${e.response.data.message}`);
          });
      } else {
        contractService.submitContract(this.props.params.id)
          .then(res => {
            if (res.status === 200) {
              this.setState({ loading: false });
              message.success(messages('common.operate.success'/*操作成功*/));
              this.onCancel();
            }
          })
          .catch(e => {
            this.setState({ loading: false });
            message.error(`${messages('common.operate.filed'/*操作失败*/)}，${e.response.data.message}`);
          });
      }
    } else {
      message.error(messages('my.contract.add.info.tips'));
      this.setState({ loading: false });
    }
  };

  //删除
  onDelete = () => {
    this.setState({ dLoading: true });
    contractService.deleteContract(this.props.params.id)
      .then(res => {
        if (res.status === 200) {
          this.setState({ dLoading: false });
          message.success(messages('common.delete.success', { name: '' }/*删除成功*/));
          this.onCancel();
        }
      })
      .catch(e => {
        this.setState({ dLoading: false });
        message.error(`${messages('common.operate.filed'/*操作失败*/)}，${e.response.data.message}`);
      });
  };

  //取消
  onCancel = () => {
    this.context.router.push(this.state.myContract.url);
  };

  render() {

    const { loading, dLoading, submitAble } = this.state;
    return (
      <div className="contract-detail background-transparent">
        <ContractDetailCommon id={this.props.params.id}
                              wrappedComponentRef={ref => this.detail = ref}
                              getContractStatus={this.getStatus}
                              isApprovePage={this.props.params.from !== 'contract'}/>
        {submitAble && (
          <Affix offsetBottom={0} className="bottom-bar-jsq">
            <Button type="primary" onClick={this.onSubmit} loading={loading} className="button-left-all">
              {messages('my.contract.submit'/*提 交*/)}
            </Button>
            <Button onClick={this.onDelete} loading={dLoading} className="button-left-half">
              {messages('my.contract.delete.contract'/*删除合同*/)}
            </Button>
            <Button onClick={this.onCancel} className="button-left-half">
              {messages('common.back'/*返回*/)}
            </Button>
          </Affix>
        )}
        {this.props.params.refund ? '' : !submitAble && (
          <Affix offsetBottom={0} className="bottom-bar-jsq">
            <Button className="button-left-all" onClick={this.onCancel}>
              {messages('common.back'/*返回*/)}
            </Button>
          </Affix>
        )}
      </div>
    );
  }
}

ContractDetail.contextTypes = {
  router: React.PropTypes.object
};

const wrappedContractDetail = Form.create()((ContractDetail));

function mapStateToProps(state) {
  return {
    user: state.login.user,
  };
}

export default connect(mapStateToProps, null, null, { withRef: true })((wrappedContractDetail));

//export default wrappedContractDetail;
